//
//  ViewController.swift
//  Entrance
//
//  Created by chunhuiLai on 2018/1/10.
//  Copyright © 2018年 chunhuiLai. All rights reserved.
//

import UIKit
//import YourFilm


typealias MenuAction = (title: String, action: Selector)

class ViewController: UIViewController {
    
    var datasource: [MenuAction] {
        return [
            (title: "To keychain", action: #selector(ViewController.showKeychainView)),
            (title: "card recognize", action: #selector(ViewController.showCardRecognize)),
            (title: "showHUD", action: #selector(ViewController.showHUD)),
            (title: "showAlert", action: #selector(ViewController.showAlert)),
            (title: "face recognize", action: #selector(ViewController.showFaceRecognize)),
            (title: "segue to phoneRechare", action: #selector(ViewController.showPhoneRecharge))
        ]
    }
    
    lazy var tableView: UITableView = {
        let tableView = UITableView.init(frame: CGRect.zero, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    func setupUI() {
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "menuCell")
        view.addSubview(tableView)

        tableView.snp.makeConstraints { (make) in
            make.top.left.bottom.right.equalTo(0)
        }
    }

    @objc func showKeychainView() {
        let keychain = KeychainViewController()
        self.navigationController?.pushViewController(keychain, animated: true)
    }
    
    @objc func showHUD() {
//        let character = HUD.init(content: HUDContent.label("从热会呢欧模vormdfdfidioiidiininiini已你的您日涅i单独的覅哦哦节热鹅鹅鹅鹅鹅鹅鹅鹅鹅鹅鹅鹅鹅鹅鹅鹅鹅鹅鹅鹅鹅鹅鹅鹅n"))
//        YourFilm.show(character)
    }
    
    @objc func showAlert() {
//        let alert = AlertView.init(title: "慢慢", message: "滚滚滚", preferredStyle: .actionSheet)
//        let action = AlertAction.init(title: "确定", handler: { _ in
//            printlog("ok")
//        })
        
//        alert.addTextField { (field) in
//            field.placeholder = "请输入来来来"
//            field.borderStyle = .roundedRect
//        }
//
//        alert.addTextField { (field) in
//            field.placeholder = "请就哦啊额偶奇偶金"
//            field.borderStyle = .roundedRect
//        }
        
//        alert.addAction(action)
//        
//        let action1 = AlertAction.init(title: "取消", handler: { _ in
//            printlog("cancel()")
//            YourFilm.curtainCall()
//        })
//        
//        alert.addAction(action1)
//        
////        let destructive = AlertAction.init(title: "destructive", handler: { _ in
////            printlog("destructive")
////        })
////        
////        alert.addAction(destructive)
//        var plot = Plot.default
//        plot.showTimeDuration = TimeInterval.infinity
//        YourFilm.show(alert, plot: plot)

        //self.present(alert, animated: true, completion: nil)
    }
    
    @objc func showCardRecognize() {
        
        //let ocrView = OCRViewControlller()
        //navigationController?.pushViewController(ocrView, animated: true)
    }
    
    @objc func showFaceRecognize() {
        let faceRecognize = CaptureViewController()
        navigationController?.pushViewController(faceRecognize, animated: true)
    }
    
    @objc func showPhoneRecharge() {
        let phoneRecharge = PhoneRechargeRouter.buildModule()
        navigationController?.pushViewController(phoneRecharge, animated: true)
    }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datasource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if  let model = datasource.sy_Element(at: indexPath.row) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath)
            cell.textLabel?.text = model.title
          
            return cell
        }
        
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if let model = datasource.sy_Element(at: indexPath.row) {
            
            perform(model.action)
        }
        
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

